import os
import json
import logging
import traceback

from flask import current_app


def load_questions_data():
    """
    加载题库数据并去重：
    - 题目一样就去重
    - 或者四个选项一样（顺序无关）也去重
    """
    try:
        base_dir = os.path.dirname(os.path.abspath(__file__))
        file_path = os.path.join(base_dir, 'data', 'questions_all.json')

        if not os.path.exists(file_path):
            current_app.logger.warning("题库文件不存在")
            return []

        current_app.logger.info("正在加载题库数据...")

        with open(file_path, 'r', encoding='utf-8') as f:
            questions_list = json.load(f)

        current_app.logger.info(f"成功加载题库数据，原始题目数量: {len(questions_list)}")
        seen_subjects = set()
        seen_options = set()
        unique_questions = {}
        for question in questions_list:
            subject = question.get('subject', '').strip()
            options = [
                question.get('optionA', '').strip(),
                question.get('optionB', '').strip(),
                question.get('optionC', '').strip(),
                question.get('optionD', '').strip()
            ]
            options_sorted_str = '_'.join(sorted(options))

            # 去重条件1：题目一样
            if subject in seen_subjects:
                continue

            # 去重条件2：四个选项一样（顺序不管）
            if options_sorted_str in seen_options:
                continue

            options.sort()
            unique_key = f"{subject}_{'_'.join(options)}"
            unique_questions[unique_key] = question

        current_app.logger.info(f"去重题库数据，题目数量: {len(unique_questions)}")
        return list(unique_questions.values())

    except Exception as e:
        current_app.logger.error(f"加载题库数据失败: {str(e)}")
        current_app.logger.error(traceback.format_exc())
        return []
